package com;

import java.text.*;

import java.util.*; 
import java.sql.*;

import javax.swing.JOptionPane;

public class UserDAO
{
	//static Connection currentCon = null; 
	//static ResultSet rs = null; 
	
	public static UserBean login(UserBean bean) 
	{ //preparing some objects for connection
		Statement stmt = null;
		String username = bean.getUsername(); 
		String password = bean.getPassword(); 
		
		int i=0;
        
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
        Connection	   currentCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/boa", "root","");
            
                   Statement st = currentCon.createStatement();
               ResultSet     rs = st.executeQuery("Select panno,pass from register where panno='"+username+"' and pass='"+password+"'; ");
                
                   //to fetch login details entered by the user on the login form
               
                                   
                   
		   while(rs.next()) //to fetch login details from database
                   {
			   
		     username=rs.getString(1);
		     password=rs.getString(2);
		     //System.out.println(loginid);
		     //System.out.println(passwd);
                     i=1;
                   }
                                                
                   if(i==1) //to verify the log id and pwd
		   {       // JOptionPane.showMessageDialog(null,"Correct","Login Error", JOptionPane.INFORMATION_MESSAGE);
                	   System.out.println("success hi");
                	   bean.setValid(true);
                	  //System.out.println(" <html><body><form action=HomePage.jsp></form></body></html>");
                	   
                	 //  new LoginServlet();        
                     //dispose();
                        
                   } 
                 
                  else
                  {
                	  System.out.println("Unsuccessful ");
                	 // new NewServlet();
                    // JOptionPane.showMessageDialog(null,"Invalid Username or Password","Login Error", JOptionPane.ERROR_MESSAGE);
                   }                                  
                }//try
            
            catch(Exception e)
            {
             System.out.println(e);
            }                         
		
		
			
		return bean; 
		}

		public static SalesUserBean sales(SalesUserBean salesbean)
		{
			//Statement stmt=null;
			int i=0;
			
			String purordnum=salesbean.getSalesOrderNumber(); 
			String invnum=salesbean.getInvoiceNumber(); 
			String dte=salesbean.getSalesDate(); 
			String custname=salesbean.getCustomerName(); 
			String custcode=salesbean.getCustomerCode(); 
			String custinvnum=salesbean.getCustomerInvoiceNumber(); 
			String prodname=salesbean.getProductName(); 
			int prodquantity=salesbean.getProductQuantity();
			double prodrate=salesbean.getProductRate(); 
			double netamount=salesbean.getNetAmount(); 
			double taxcat=salesbean.getTaxationCategory(); 
			double taxamount=salesbean.getTaxAmount(); 
			double transport=salesbean.getTransportCharges(); 
			double totalamount=salesbean.getTotalAmount(); 
			int credit=salesbean.getCreditLimit(); 
			
			 Connection con=null;
			 // PreparedStatement ps=null;
			Statement st=null;
			  try
			  {

				    
				Class.forName("com.mysql.jdbc.Driver");
				con=DriverManager.getConnection("jdbc:mysql://localhost/BOA","root","root");
				st = con.createStatement();
			  }
				catch (SQLException e) 
				{
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				//String insertsupp = "insert into SupplierMaster values('" + sname + "','" + scode+ "','" + saddr + "','" + scity + "','" + sstate + "','" + stin + "','" + span + "','" + smob + "','" + semail + "','" + sper + "','" + sball + "')";
						
				String insertsupp = "insert into SalesVoucher(innum,date,scname ,sccode , stname ,pquantity,prate ,samount , staxcat,stamount,strans,stota,scredlim)" + "values('" + invnum + "','"+dte+"','"+custname+"','"+custcode+"','"+prodname+"','"+prodquantity+"','"+prodrate+"','"+netamount+"','"+
				taxcat+"','"+taxamount+"','"+transport+"','"+totalamount+"','"+credit+"')";
				try 
				{
					i = st.executeUpdate(insertsupp);
				}
				catch (SQLException e) 
				{
					e.printStackTrace();
				}
	
					
				return salesbean; 
		}
			/*  public static PurchaseUserBean purchase(PurchaseUserBean purchasebean)
				{ 
				  String value1=purchasebean.getPurchaseOrderNumber(); 
	    		  String value2=purchasebean.getPurchaseInvoiceNumber();
		
	    		  String value3=purchasebean.getPurchaseDate();
	    		  String value4=purchasebean.getSupplierName();
		
	    		  String value5=purchasebean.getSupplierCode();
	    		  String value6=purchasebean.getSupplierInvoiceNumber();
	    		  String value7=purchasebean.getProductName();
	    		  int value8=purchasebean.getPurchaseProdQuantity();
	    		  double value9=purchasebean.getPurchaseProdRate();
	    		  //Double value9=Double.parseDouble(t9.getText());
	    		  double value10=purchasebean.getPurchaseNetAmount();
	    		  double value11=purchasebean.getPurchaseTaxCategory();
	    		  double value12=purchasebean.getPurchaseTransport();
	    		  double value13=purchasebean.getPurchaseOtherCosts();
	    		  int value14=purchasebean.getPurchaseCreditLimit();
		
		
	       	Connection con=null;
	      	PreparedStatement ps=null;

	       try
	       {
	    	   Class.forName("com.mysql.jdbc.Driver");
	    	   con=DriverManager.getConnection("jdbc:mysql://localhost/BOA","root","root");
	    	   ps=con.prepareStatement("insert into PurchaseVoucher values(?,?,?,?,?,?,?,?,?,?,?,?,?,?);");

	    	   //(pvinno, date, pvsname, pvscode, pvstname, pvqua, pvrate, pvneta, pvtaxcat, pvtaxa, pvtrans, pvot, pvtota, pvcreadlim)
	    	   ps.setString(1,value1);
	    	   ps.setString(2,value2);
	    	   ps.setString(3,value3);
	    	   ps.setString(4,value4);
	    	   ps.setString(5,value5);
	    	   ps.setString(6,value6);
	    	   ps.setString(7,value7);
	    	   ps.setDouble(8,value8);
	    	   ps.setDouble(9,value9);
	    	   ps.setDouble(10,value10);
	    	   ps.setDouble(11,value11);
	    	   ps.setDouble(12,value12);
	    	   ps.setDouble(13,value13);
	    	   ps.setInt(14,value14);
		         
	     int r=ps.executeUpdate();
		 if(r==0)
		   JOptionPane.showMessageDialog(null,"Record not inserted Retry","",JOptionPane.ERROR_MESSAGE);
	     else
	       JOptionPane.showMessageDialog(null,"Record Inserted Successfully","",JOptionPane.INFORMATION_MESSAGE);
		


		}
		catch(Exception e)
		{
			System.out.println(e);
		}
			return purchasebean;
		
		}*/
	}


